package View;


import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;

import Model.Player;
import Model.MarketPlace.MarketPlaceItem;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Choice;
import javax.swing.JList;

//***Add Weapons Options***


public class SpaceshipInfoView extends JPanel implements Model.IUpdatable{
    /**
     * 
     */
    protected JTextField fuelFieldd = new JTextField();

    /**
     * 
     */
    protected JButton fuelBtn;
    
    /**
     * 
     */
    protected JButton cargoBtn;
    
    /**
     * 
     */
    protected JButton weaponsBtn;
    
    /**
     * 
     */
    protected JButton shieldBtn;
    
    /**
     * 
     */
    protected JTextField infoField;
    
    /**
     * 
     */
    protected int clickCount;
    
    /**
     * 
     */
    protected Choice weaponOptions;
    
    /**
     * 
     */
    protected JList<MarketPlaceItem> cargoList;
    
    /**
     * 
     */
    protected DefaultComboBoxModel<MarketPlaceItem> listModel = 
            new DefaultComboBoxModel<MarketPlaceItem>();

    /**
 * Create the panel.
 */
public SpaceshipInfoView() {
    
    this.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
            fuelBtn.setVisible(true);
            fuelFieldd.setVisible(false);
            cargoBtn.setVisible(true);
            weaponsBtn.setVisible(true);
            weaponOptions.setVisible(false);
            shieldBtn.setVisible(true);
            cargoList.setVisible(false);
            infoField.setText("Welcome to the Ship Hub.");
        }
   
    });

    setLayout(null);
    //Fuel Text Box
    fuelFieldd.setFont(new Font("Lucida Console", Font.PLAIN, 11));
    fuelFieldd.setForeground(new Color(0, 255, 0));
    fuelFieldd.setBackground(new Color(0, 0, 0));
    fuelFieldd.setToolTipText("Enter the amount of fuel you wanna add...");
    fuelFieldd.setBounds(10, 58, 86, 20);
    add(fuelFieldd);
    fuelFieldd.setColumns(10);
    fuelFieldd.setVisible(false);

    //Fuel Button
    fuelBtn = new JButton("Fuel");
    fuelBtn.setBackground(new Color(255, 204, 0));
    fuelBtn.setToolTipText("Buy as much fuel as you need.");
    fuelBtn.setBounds(10, 11, 89, 23);
    add(fuelBtn);
    //**notify of current fuel**
    fuelBtn.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
           clickCount++;
           fuelFieldd.setVisible(true);
           infoField.setText("2 Credits per gallon. Enter amount.");
           final Player player = Player.getInstance();
           if(clickCount > 1 && fuelFieldd.getText() == ""){
               try{
                   final double fuel = Double.parseDouble(fuelFieldd.getText());
                   final double credits = fuel * 2.0;
                   if(player.getShip().getFuel() + fuel <= player.getShip().getMaxFuel()){
                       if(player.getCredits() - credits > 0){
                           infoField.setText("***Not enough credits.***");
                       }
                       infoField.setText(fuel + " units of fuel sold.");
                       player.getShip().setFuel(player.getShip().getFuel() + fuel);
                       player.setCredits(player.getCredits() - credits);
                       System.out.println(fuel);
                   }
                   else infoField.setText("**You do not have enough fuel space**");
                   
               }
               catch(NumberFormatException ex){
                   infoField.setText("Invalid input. Try again.");
               }
               
           }
           cargoBtn.setVisible(false);
           weaponsBtn.setVisible(false);
           shieldBtn.setVisible(false);
           weaponOptions.setVisible(false);
        }

    });
    
    //Weapons Button
    weaponsBtn = new JButton("Weapons");
    weaponsBtn.setToolTipText("Bigger guns. Chicks dig guns.");
    weaponsBtn.setBackground(new Color(255, 204, 0));
    weaponsBtn.setBounds(10, 325, 89, 23);
    add(weaponsBtn);
    
    weaponsBtn.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
            weaponOptions.setVisible(true);

        }

    });
    
    //Cargo Button
    cargoBtn = new JButton("Cargo");
    cargoBtn.setToolTipText("Make your ship the best half-ton in the galaxy. ");
    cargoBtn.setBackground(new Color(255, 204, 0));
    cargoBtn.setBounds(375, 325, 89, 23);
    add(cargoBtn);
    
    cargoBtn.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
            weaponsBtn.setVisible(false);
            fuelBtn.setVisible(false);
            shieldBtn.setVisible(false);
            final List<MarketPlaceItem> stuff = Player.getInstance().getShip().getCargoItems();
            for(MarketPlaceItem item: stuff){
                listModel.addElement(item);
            }
            cargoList.setVisible(true);
            weaponOptions.setVisible(false);
            
        }

    });
    
    //Shield Button
    shieldBtn = new JButton("Shields");
    shieldBtn.setToolTipText("Upgrade your shields...it's rough out there.");
    shieldBtn.setBackground(new Color(255, 204, 0));
    shieldBtn.setBounds(375, 11, 89, 23);
    add(shieldBtn);

    shieldBtn.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
            weaponOptions.setVisible(false);
        }

    });
    
    final ButtonGroup group = new ButtonGroup();
    group.add(fuelBtn);
    group.add(weaponsBtn);
    group.add(cargoBtn);
    group.add(shieldBtn);
    
    //Info Bar
    infoField = new JTextField();
    infoField.setToolTipText("FYI.");
    infoField.setEditable(false);
    infoField.setForeground(new Color(0, 255, 0));
    infoField.setFont(new Font("Lucida Console", Font.PLAIN, 11));
    infoField.setBackground(Color.BLACK);
    infoField.setBounds(107, 12, 256, 20);
    infoField.setHorizontalAlignment(JTextField.CENTER);
    add(infoField);
    infoField.setColumns(10);
    infoField.setText("Welcome to the Ship Hub.");
    
    //Weapon Options
    weaponOptions = new Choice();
    weaponOptions.setForeground(new Color(0, 255, 0));
    weaponOptions.setBackground(new Color(0, 0, 0));
    weaponOptions.setBounds(109, 325, 256, 20);
    weaponOptions.add("Simple Blaster Upgrade");
    weaponOptions.add("Pulse Laser");
    weaponOptions.add("MAC Cannon");
    add(weaponOptions);

    //Cargo List
    cargoList = new JList<MarketPlaceItem>(listModel);
    cargoList.setForeground(new Color(255, 255, 255));
    cargoList.setBackground(new Color(0, 0, 0));
    cargoList.setBounds(164, 59, 142, 168);
    add(cargoList);
    weaponOptions.setVisible(false);
    
    
}

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    BufferedImage image = null;
    try{
        image = ImageIO.read(new File("src\\View\\spaceship.jpg"));
        
    }
    catch(IOException e){
    	System.out.println(e.getMessage());
    	}
        
    g.drawImage(image, 0, 0, null);
    
}

@Override
public void update() {
    clickCount = 0;
    infoField.setText("Welcome to the Ship Hub");
    fuelBtn.setVisible(true);
    fuelFieldd.setVisible(false);
    fuelFieldd.setText("");
    weaponsBtn.setVisible(true);
    weaponOptions.setVisible(false);
    cargoBtn.setVisible(true);
    cargoList.setVisible(false);
    shieldBtn.setVisible(true);
    
}
}
